{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>高斯公式<h1>                                                     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高斯（Gauss）公式表达了空间闭区域上的三重积分与其边界曲面的曲面积分之间的关系"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定理1：设空间闭区域 $\\Omega$ 是由分片光滑的闭曲面 $\\Sigma$ 所围成，若函数 P(x,y,z)、Q(x,y,z)与 R(x,y,z)在 $\\Omega$ 上具有一阶连续偏导数，则有\n",
    "\n",
    "\\begin{equation}\n",
    "\\iiint \\limits_{\\Omega}(\\frac{\\partial P}{\\partial x}+\\frac{\\partial Q}{\\partial y}+\\frac{\\partial R}{\\partial z})\\mathrm{d}t = \n",
    "\\oiint \\limits_{\\Sigma} Pdxdy + Qdxdz +Rdydz \\notag\n",
    "\\end{equation}\n",
    "\n",
    "或\n",
    "\n",
    "\\begin{equation}\n",
    "\\iiint \\limits_{\\Omega}(\\frac{\\partial P}{\\partial x}+\\frac{\\partial Q}{\\partial y}+\\frac{\\partial R}{\\partial z})\\mathrm{d}t = \n",
    "\\oiint \\limits_{\\Sigma}(Pcos\\alpha + Qcos\\beta + Rcos\\gamma)dS  \\notag\n",
    "\\end{equation}\n",
    "\n",
    "这里 $\\Sigma$ 是 $\\Omega$ 整个边界曲面的外侧， $cos\\alpha$、$Qcos\\beta$ 和 $cos\\gamma$ 是 $\\Sigma$ 在(x,y,z)处的法向量的余弦，上述两个公式即是高斯公式。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "例二(p234)\n",
    "利用高斯公式计算曲面积分\n",
    "\n",
    "$\\begin{equation}\n",
    "\\oiint \\limits_{\\Sigma}(x-y)dxdy + (y-z)xdydz,  \\notag\n",
    "\\end{equation}$\n",
    "\n",
    "其中 $\\Sigma$ 是柱面 $x^2+y^2=1$ 及平面 $z=0,z=3$ 所围成的空间闭合区域 $\\Omega$ 的整个边界曲面的外侧(图11-26)\n",
    "\n",
    "![Desktop Screenshot 2024.12.08 - 00.28.59.33(1).png](<attachment:Desktop Screenshot 2024.12.08 - 00.28.59.33(1).png>)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用高斯公式将所给曲面积分转化为三重积分，再利用柱面坐标计算三重积分，得\n",
    "\n",
    "$\\begin{align}\n",
    "\\oiint \\limits_{\\Sigma}(x-y)dxdy + (y-z)xdydz \\notag \\\\\n",
    "=\\iiint \\limits_{\\Sigma}(y-z)dxdydz = \\iiint \\limits_{\\Sigma}(\\rho sin\\theta -z)\\rho d\\rho d\\theta dz \\notag\\\\\n",
    "=\\int^{2\\pi}_{0}d\\theta \\int^{1}_{0}\\rho d\\rho \\int^{3}_{0}(\\rho sin\\theta -z)dz \\notag\n",
    "=-\\frac{9\\pi}{2} \\notag\n",
    "\\end{align}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAD0AAAASCAYAAADomNYKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAABJ0AAASdAHeZh94AAADVUlEQVR4nOXXW4hXVRQH4G8aCwWpLKKgKLSCQKNRqF6imxGVZM5E1IMm9RBEJFJRFMhqBYUPSfUgBSnaQ5FDd7ILglCJVBCMWWJ00QcRCwsky8xbD/ucOB7/owMeAmnBYZ3z23uvy15rr71O36FDh/zfaFyXwjKzD/fgPkxFP77DCiyNiAMd6job27EUiUHMwqU4F39jY6V7RUQcrNee1JURFb2C5ZiMVXgZp+AFrKo2pSu6TbH/bdxR6boSX+B5vIlpWIbhpu7OIp2ZczAPW3BFROys8JMxjNsxHys7UjmIX/Ep+jAbq5sRzcwn8GWle0jZiE7Te6jiS2qHISL2ZeYizMGDGk5n5iy8P0b50yNipFp3Gq7Ha9WRWdtrQUTsyMyX8DSuVTndZXqfU/GfeozV2IzMPL2B78KL1fu3ytmsn1UVvg6BrxvrZinH5q0x2LWv4vtroMtI19Gd3GNsSuP9EnwOEbEuM8/C/XgnIp6sJ2Xmo7gTKyNieUveIP7AmqMZlJnjcHf1+VGNdxnpOk0fyswzWoqzMW9Sa91AxUda+IxeeGaOx034MCL+OoZNi5Vi9kFEfFyDh0U6M7figmMIatKrETG3en8dc3EzNmXme/gTN+BCfI+L0b62Biq+oYXPUFLymxZ+IyYqVXtUyswFeBiblQL7L7Uj/aNyr4712V4vrKrmbDyCHZWie7ENVymVFn5p6RzAbvzQMHgiLsLmiNjbmj+o3MGrj+LwA8o1uQnXRcRvzfHDIh0RM0cTNBaKiP1YUj1NIyYozu1RClaNT8L5WB8RzdZwunINjbTk9ONWrI2IXb1syMyFeE7JkJkR0d7kzpuT0WgexmM4IvY18IGK90ptjjznV+NMo6R2Zj6mODyiRPgIh+nY6cw8tQd2uVJQduOp1vBAxUda+LSKb2zhQziId3voWVTp+UqJ8M72nJo67b2xJjP3KKn1u9J/34K9GIqI9h0+UPF2pOvqPzUzN0XEtup7jnIUfm5Ozsz5yoYewGdYkNm8MMDWiFhJ906/gbuUKj5BKXTLsDgitvaYf5kSuXZEh3GN0kn149kqY85T0rdNdW/Qj4Wj2PaJqhvsO1F+LTPzGTyOKRGx5Xhk/VeFrAsaxIbjdZgTKNJd0j/qbiDKMe/mcAAAAABJRU5ErkJggg==",
      "text/latex": [
       "$\\displaystyle - \\frac{9 \\pi}{2}$"
      ],
      "text/plain": [
       "-9⋅π \n",
       "─────\n",
       "  2  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "from sympy.abc import x,y,z\n",
    "import math\n",
    "x,y,z=sympy.symbols('x y z')\n",
    "P=sympy.Lambda((x,y,z),(y-z)*x)\n",
    "Q=sympy.Lambda((x,y,z),0)\n",
    "R=sympy.Lambda((x,y),x-y) \n",
    "#分析原函数对应高斯公式找出对应P,Q,R函数\n",
    "\n",
    "P.diff(x)\n",
    "Q.diff(y)\n",
    "R.diff(z)\n",
    "#分别求偏导\n",
    "\n",
    "#利用上述三重积分将原式转化成三重积分\n",
    "rho,theta=sympy.symbols('rho theta')\n",
    "ans1=sympy.integrate(rho*sympy.sin(theta)-z,(z,0,3))\n",
    "ans2=sympy.integrate(ans1*rho,(rho,0,1))\n",
    "ans=sympy.integrate(ans2,(theta,0,2*sympy.pi)) #依次计算积分\n",
    "sympy.init_printing()                          #使用sympy表达式打印\n",
    "ans"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例二(p234)利用高斯公式计算曲面积分\n",
    "$\\begin{equation}\n",
    "\\iint \\limits_{\\Sigma}(x^2 cos\\alpha + y^2 cos\\beta +z^2 cos\\gamma)dS,\n",
    "\\end{equation}$\n",
    "其中 $\\Sigma$ 为锥面 $x^2+y^2=z^2$ 介于平面 $z=0$ 及平面 $z=h(h>0)$ 之间的部分的下侧，$cos\\alpha cos\\beta cos\\gamma$ 是 $\\Sigma$ 在点(x,y,z)处的法向量的余弦。 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>解</b>  因曲面 $\\Sigma$ 不是封闭曲面，故不能直接利用高斯公式，若设 $\\Sigma_1$ 为 $z=h(x^2+y^2 \\leq h^2)$ 的上侧，则 $\\Sigma$ 与 $\\Sigma_1$ 一起构成一个封闭曲面，记它们围成的空间闭区域为 $\\Omega$ ，利用高斯公式，便得到\n",
    "\\begin{split}\n",
    "\\oiint \\limits_{\\Sigma+\\Sigma_1}(x^2 cos \\alpha +y^2 cos \\beta +z^2 cos \\gamma)dS \\notag \\\\\n",
    "=\\iiint \\limits_{\\Omega}(x+y+z)dv =2 \\iint \\limits_{D_{xy}}dxdy \\int^{h}_{\\sqrt{x^2+y^2}}(x+y+z)dz \\notag\n",
    "\\end{split}\n",
    "\n",
    "其中 $D_xy={(x,y)|x^2+y^2 \\leq h^2 } $ 由于积分的对称性\n",
    "\\begin{split}\n",
    "\\iint \\limits_{D_{xy}} dxdy \\int ^{h}_{\\sqrt{x^2+y^2}} (x+y)dz =0  \\notag\n",
    "\\end{split}\n",
    "\n",
    "得到\n",
    "\n",
    "$\\begin{equation}\n",
    "\\oiint \\limits_{\\Sigma+\\Sigma_1}(x^2 cos \\alpha +y^2 cos \\beta +z^2 cos \\gamma)dS = \\iint \\limits_{D_{xy}} (h^2-x^2-y^2)dxdy= \\frac{1}{2}\\pi h^4 \\notag\n",
    "\\end{equation}$\n",
    "\n",
    "而\n",
    "\n",
    "$\\begin{equation}\n",
    "\\iint \\limits_{\\Sigma}(x^2 cos\\alpha + y^2 cos\\beta +z^2 cos\\gamma)dS = \\iint \\limits_{\\Sigma_1} z^2 dS = \\iint \\limits_{D_{xy}} h^2 dxdy = \\pi h^4 \\notag\n",
    "\\end{equation}$\n",
    "\n",
    "因此\n",
    "\n",
    "$\\begin{equation}\n",
    "\\iint \\limits_{\\Sigma}(x^2 cos\\alpha + y^2 cos\\beta +z^2 cos\\gamma)dS = \\frac{1}{2}\\pi h^4 - \\pi h^4 = -\\frac{1}{2}\\pi h^4 \\notag\n",
    "\\end{equation}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAVCAYAAAAU9vPjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAABJ0AAASdAHeZh94AAADqElEQVR4nO3XW4hXdRAH8M+6JoKKVEQPRZEUEhmuK5HRHSMC6bJG9NDNCB+iEisoEmyaIOmhqCgpuqAESkmmRWYXELxQGCprpkVl+RBCUqKQUFpuD+f863Tc/97820o0cJhzfpeZOXO+853faevp6fG/9C4jhzuAo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ip1XyJ6srMdLpGy8GAAAAAElFTkSuQmCC",
      "text/latex": [
       "$\\displaystyle - \\frac{\\pi h^{4}}{2}$"
      ],
      "text/plain": [
       "    4 \n",
       "-π⋅h  \n",
       "──────\n",
       "  2   "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "x,y,z,h,S=sympy.symbols('x y z h S')\n",
    "alpha,beta,gamma=sympy.symbols('alpha beta gamma')\n",
    "rho,theta=sympy.symbols('rho theta')\n",
    "P=sympy.Lambda((x,alpha),x**2*sympy.cos(alpha))\n",
    "Q=sympy.Lambda((y,beta),y**2*sympy.cos(beta))\n",
    "R=sympy.Lambda((z,gamma),z**2*sympy.cos(gamma))\n",
    "P.diff(x)\n",
    "Q.diff(y)\n",
    "R.diff(z)\n",
    "\n",
    "sympy.init_printing()\n",
    "I1=sympy.integrate((h**2-rho**2)*rho,(rho,0,h))\n",
    "I=sympy.integrate(I1,(theta,0,2*sympy.pi))     #将曲面积分转化成极坐标计算\n",
    "\n",
    "T1=sympy.integrate(h**2*rho,(rho,0,h))\n",
    "T=sympy.integrate(T1,(theta,0,2*sympy.pi))\n",
    "\n",
    "ans=I-T\n",
    "ans"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
